﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UserModels;
using UserModels.DTO;
using UserRepositorys.Intface;

namespace UserRepositorys.Impl
{
    public class MenuRepository : Repository<MenuInfoModel>, IMenuRepository
    {
        private readonly IAccountRepository accountRepository;
        private readonly IRoleRepository roleRepository;
        private readonly ITiesRepository tiesRepository;
        private readonly IAccountRoleRepository accountRoleRepository;
        public MenuRepository(ISqlSugarClient db, IAccountRepository accountRepository, IRoleRepository roleRepository, ITiesRepository tiesRepository, IAccountRoleRepository accountRoleRepository) : base(db)
        {
            this.accountRepository = accountRepository;
            this.roleRepository = roleRepository;
            this.tiesRepository = tiesRepository;
            this.accountRoleRepository = accountRoleRepository;
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menuInfo"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> AddMenus(MenuInfoModel menuInfo)
        {
            return await base.Addasync(menuInfo);
        }

        /// <summary>
        /// 第一级菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<MenuTree>> GetMenus(int id = 0, int accountId = 0)
        {
            var accountRole = await accountRoleRepository.GetAllasync();
            var account = await accountRepository.GetAllasync();
            var role = await roleRepository.GetRoleAllList();
            var menuList = await base.GetAllAsync(x => x.ParentId == id);
            var ties = await tiesRepository.GetAllasync();
            var treelist = (from a in account
                           join b in accountRole on a.AccountId equals b.AccountId
                           join c in role on b.RoleId equals c.RoleId
                           join d in ties on c.RoleId equals d.RoleId
                           join e in menuList on d.MenuId equals e.MenuId
                           where a.AccountId == accountId
                           select e).Distinct();

            var menuTree = treelist.OrderBy(x => x.MenuId).Select(x => new MenuTree
            {
                value = x.MenuId,
                url = x.MenuUrl,
                label = x.MenuName,
            }).Distinct();
            return menuTree.ToList();
        }

        /// <summary>
        /// 菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<MenuTree>> GetMenuTrees(int id = 0)
        {
            var menuList = await base.GetAllAsync(x => x.ParentId == id);

            var list = new List<MenuTree>();
            foreach (var menu in menuList)
            {
                var children = await base.GetAllAsync(x => x.ParentId == menu.MenuId);
                if (children.Any())
                {
                    var subMenuTrees = await GetMenuTrees(menu.MenuId);
                    var menuTree = new MenuTree
                    {
                        value = menu.MenuId,
                        url = menu.MenuUrl,
                        label = menu.MenuName,
                        children = subMenuTrees
                    };
                    list.Add(menuTree);
                }
                else
                {
                    var menuTree = new MenuTree
                    {
                        value = menu.MenuId,
                        url = menu.MenuUrl,
                        label = menu.MenuName
                    };
                    list.Add(menuTree);
                }
            }
            return list;
        }
    }
}
